package com.breeze.components.mybatis.util;

import cn.hutool.core.collection.CollUtil;
import com.breeze.components.core.util.NumberUtils;
import com.breeze.components.mybatis.page.AbstractPageQuery;
import com.mybatisflex.core.paginate.Page;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author breeze
 */
public class PageUtil {

  public static <T, R> Page<R> convertPage(Page<T> page, Function<? super T, ? extends R> mapper) {
    Page<R> ret = new Page<>();
    ret.setTotalRow(page.getTotalRow());
    ret.setPageSize(page.getPageSize());
    ret.setPageNumber(page.getPageNumber());
    ret.setRecords(page.getRecords().stream().map(mapper).collect(Collectors.toList()));
    return ret;
  }

  public static <T> boolean isEmpty(Page<T> page) {
    return Objects.isNull(page) || CollUtil.isEmpty(page.getRecords());
  }

  public static int getPageSize(int limit) {
    return NumberUtils.isNumberAndGt(limit, 0) ? limit : AbstractPageQuery.DEFAULT_PAGE_SIZE;
  }
}
